// 存储到本地的值的名称，需要复杂一些以防和其他软件重名
const INFO_KEY = 'hm_shopping_info'
const SEARCH_HISTORY = 'hm_shopping_search_history'

// 获取用户信息
export const setItem = (info) => {
  localStorage.setItem(INFO_KEY, JSON.stringify(info))
}
// 设置用户信息
export const getItem = () => {
  const result = localStorage.getItem(INFO_KEY)
  // 正常返回查找到的内容的对象 如果获取不到本地的用户信息，返回默认值
  return result ? JSON.parse(result) : { token: '0000', userId: '0' }
}
// 清空用户信息
export const removeItem = () => {
  localStorage.removeItem(INFO_KEY)
}

// 获取历史记录
export const getSearchHistory = () => {
  const result = localStorage.getItem(SEARCH_HISTORY)
  return result ? JSON.parse(result) : []
}
// 存储历史记录
export const setSearchHistory = (history) => {
  localStorage.setItem(SEARCH_HISTORY, JSON.stringify(history))
}
// 清空历史记录
export const removeSearchHistory = () => {
  localStorage.removeItem(SEARCH_HISTORY)
}
